﻿using Minecraft.Config;
using Minecraft.Model;
using Minecraft.Model.Blueprint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minecraft.ServerHall
{
	public static class BlueprintExt
	{
		/// <summary>
		/// 获取蓝图信息列表
		/// </summary>
		/// <param name="blueprint"></param>
		/// <returns></returns>
		public static List<BlueprintInfo> GetBlueprintInfoList(this BlueprintModel blueprint)
		{
			if (blueprint.BlueprintIdAndStudyStr.Contains("|"))
			{
				List<BlueprintInfo> blueprintInfoList = new List<BlueprintInfo>();
				var itemStrs = blueprint.BlueprintIdAndStudyStr.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
				foreach (var itemStr in itemStrs)
				{
					var items = itemStr.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
					BlueprintInfo blueprintInfo = new BlueprintInfo
					{
						blueprintId = items[0],
						//Position = CsvConfig.blueprintList.FirstOrDefault(m => m.Id == items[0]).Place_Convert,
						Position = CsvConfig.GetMod_BlueprintInfoById(items[0]).Place_Convert,
						isStudied = items[1] == "1" ? true : false
					};
					blueprintInfoList.Add(blueprintInfo);
				}
				blueprintInfoList = blueprintInfoList.OrderBy(m => m.Position).ToList();
				return blueprintInfoList;
			}
			else
			{
				return new List<BlueprintInfo>();
			}
		}

		/// <summary>
		/// str转换
		/// </summary>
		/// <param name="blueprint"></param>
		/// <param name="blueprintInfo"></param>
		public static void TransformToBlueprintIdAndStudyStr(this BlueprintModel blueprint, BlueprintInfo blueprintInfo)
		{
			var list = blueprint.GetBlueprintInfoList();
			var index = list.FindIndex(m => m.blueprintId == blueprintInfo.blueprintId);
			if (index >= 0)
			{
				list.RemoveAt(index);
			}
			list.Add(blueprintInfo);
			list = list.OrderBy(m => m.Position).ToList();
			blueprint.BlueprintIdAndStudyStr = string.Join("#", list.ToList().ConvertAll(info =>
			{
				return string.Join("|", info.blueprintId, info.isStudied ? "1" : "0");
			}).ToArray());
		}
	}
}
